package com.winit.openapi.wop.inbound.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.ValidateUtil;
import com.winit.openapi.wop.inbound.constant.WHInboundConstant;
import com.winit.pms.spi.winit.LogisticsdistService;
import com.winit.pms.spi.winit.WinitProductService;
import com.winit.pms.spi.winit.command.QueryWarehouseLogisticsDistCommand;
import com.winit.pms.spi.winit.command.WinitProductQueryCommand;
import com.winit.pms.spi.winit.vo.LogisticsDist;
import com.winit.pms.spi.winit.vo.WinitProduct;

@Controller
@RequestMapping(value = "/winit/pms")
public class WHInboundWareHouseController extends BaseController {

    @Resource
    private LogisticsdistService logisticsdistService;

    @Resource
    private WinitProductService  productService;

    /**
     * 根据条件查询验货仓库/目的仓库列表 warehouseType warehouseType标示查询的是验货仓还是目的仓：
     * 
     * @param 验货仓类型值：INSJ
     * @param 目的仓类型值：DEST
     * @param requestMsg
     * @param responseMsg
     * @return
     * @throws ParseException
     */
    @RequestMapping(value = "/getWarehouseList")
    public String getWarehouseList(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg)
                                                                                                                  throws ParseException {
        Object obj = requestMsg.getData();
        QueryWarehouseLogisticsDistCommand command = new QueryWarehouseLogisticsDistCommand();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ValidateUtil.validNotNull(json, "winitProductCode");
            ValidateUtil.validNotNull(json, "warehouseType");
            ValidateUtil.validNotNull(json, "orderType");
            ValidateUtil.validMaxLength(json, "winitProductCode", 50, true);
            ValidateUtil.validMaxLength(json, "warehouseType", 50, true);
            ValidateUtil.validMaxLength(json, "orderType", 20, true);

            String winitProductCode = json.getString("winitProductCode");
            WinitProductQueryCommand productCommand = new WinitProductQueryCommand();
            productCommand.setCode(winitProductCode);
            WinitProduct product = productService.queryProduct(productCommand);
            if (null == product) {
                throw new ApiException(ErrorCode.WINIT_PRODUCT_NOT_EXISTS, winitProductCode);
            }

            command.setProductCode(json.getString("winitProductCode"));
            if (!WHInboundConstant.InboundOrderType.isValidOrderType(json.getString("orderType"))) {
                throw new ApiException(ErrorCode.FEILD_NOT_MATCH, "orderType");
            }
            // 直发海外验的不需要填验货仓
            if (!"DW".equals(json.getString("orderType"))) {

                //ValidateUtil.validNotNull(json, "inspectionWarehouseCode");
                //ValidateUtil.validMaxLength(json, "inspectionWarehouseCode", 50, true);
                if (WHInboundConstant.WarehouseType.INSJ.getCode().equalsIgnoreCase(json.getString("warehouseType"))) {
                    command.setDistType("Warehouse");
                } else if (WHInboundConstant.WarehouseType.DEST.getCode()
                    .equalsIgnoreCase(json.getString("warehouseType"))) {
                    // 当warehouseType=DEST时，必填
                    ValidateUtil.validNotNull(json, "inspectionWarehouseCode");
                    ValidateUtil.validMaxLength(json, "inspectionWarehouseCode", 50, true);
                    command.setDistType("DestWarehouse");
                } else {
                    throw new ApiException(ErrorCode.FEILD_NOT_MATCH, "warehouseType");
                }
            } else {
                command.setDistType("DestWarehouse");
            }

        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        List<LogisticsDist> distList = logisticsdistService.queryWarehouseList(command);
        List<Map<String, String>> whouseList = new ArrayList<Map<String, String>>();
        Map<String, Object> data = new HashMap<String, Object>();
        Map<String, String> warehouseMap = null;
        if (distList != null && distList.size() > 0) {

            for (LogisticsDist dist : distList) {
                warehouseMap = new HashMap<String, String>();
                warehouseMap.put("countryCode", dist.getCountryCode());
                warehouseMap.put("warehouseCode", dist.getCode());
                warehouseMap.put("warehouseName", dist.getName());
                whouseList.add(warehouseMap);
            }

        }
        data.put("warehouseList", whouseList);

        responseMsg.setData(data);

        return SUCCESS;
    }
}
